package fr.umlv.projet.android.utils;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;

public class ServerHelper extends AsyncTask<Void, Void, Void> {

	private static String address;
	private static String port;
	private static String token;

	/*
	 * Try to log in the server with informations.
	 * If the authentication succeed, we store the exams list in the intent.
	 */
	public static void login(Intent intent, String address_in, String port_in, String username, String password){
		String uri = new StringBuffer("http://").append(address_in).append(':').append(port_in).append("/connect").toString();

		HttpClient httpClient = new DefaultHttpClient();
		HttpContext localContext = new BasicHttpContext();
		HttpPost httpPost = new HttpPost(uri);

		try {
			MultipartEntity mentity = new MultipartEntity();
			mentity.addPart("username", new StringBody(username));
			mentity.addPart("password", new StringBody(password));
			httpPost.setEntity(mentity);

			HttpResponse response = httpClient.execute(httpPost, localContext);
			HttpEntity entity = response.getEntity();

			// Parse the response
			String responseStr = StringUtils.inputStreamToString(entity.getContent());
			JSONObject jsonObject = new JSONObject(responseStr);

			// Put exam list in intent
			JSONArray jsonArray = jsonObject.getJSONArray("exams");
			int length = jsonArray.length();
			String[] examArray = new String[length];
			for (int i=0; i<length; i++){
				examArray[i] = jsonArray.getString(i);
			}
			intent.putExtra("exams", examArray);

			// Initialize static variables
			address = address_in;
			port = port_in;
			token = jsonObject.get("token").toString();


		} catch (Exception e) {
			throw new IllegalArgumentException("Connection failed");
		}
	}

	public static void sendMessage(String exam, String message) throws Exception{
		String uri = new StringBuffer("http://").append(address).append(':').append(port).append("/message").toString();

		HttpClient httpClient = new DefaultHttpClient();
		HttpContext localContext = new BasicHttpContext();
		HttpPost httpPost = new HttpPost(uri);

		MultipartEntity entity = new MultipartEntity();
		entity.addPart("token", new StringBody(token));
		entity.addPart("exam", new StringBody(exam));
		entity.addPart("content", new StringBody(message));
		httpPost.setEntity(entity);
		
		httpClient.execute(httpPost, localContext);

	}
	
	public static void submitResponse(String exam, String data) throws Exception{
		String uri = new StringBuffer("http://").append(address).append(':').append(port).append("/submit").toString();

		HttpClient httpClient = new DefaultHttpClient();
		HttpContext localContext = new BasicHttpContext();
		HttpPost httpPost = new HttpPost(uri);
		
		MultipartEntity entity = new MultipartEntity();
		entity.addPart("token", new StringBody(token));
		entity.addPart("exam", new StringBody(exam));
		entity.addPart("data", new StringBody(data));
		httpPost.setEntity(entity);
	
		httpClient.execute(httpPost, localContext);

	}

	public static String getExam(String exam) throws ClientProtocolException, IOException {
		String uri = new StringBuffer("http://").append(address).append(':').append(port).append("/exam").append("?token=").append(token).append("&exam=").append(exam).toString();
		
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(uri);
		
		HttpResponse response = httpClient.execute(httpGet);
		HttpEntity entity = response.getEntity();

		String responseStr = StringUtils.inputStreamToString(entity.getContent());
		return responseStr;
	}
	
	@Override
	protected Void doInBackground(Void... params) {
		return null;
	}
	
}
